package service.room;

import dao.community.CommunityDao;
import dao.community.CommunityDaoImpl;
import dao.room.RoomDao;
import dao.room.RoomDaoImpl;
import domain.Community;
import domain.Page;
import domain.Room;
import util.DButils;
import util.EncryptUtil;

import java.util.List;
import java.util.ArrayList;

public class RoomServiceImpl implements RoomService {
    RoomDao roomDao = new RoomDaoImpl();
    /**
     * 验证username是否重复
     * @param username
     * @return boolean
     * @author 夏添
     * @date 2020/7/15 21:33
     */
    @Override
    public boolean ValidateName(String username){
        boolean isCreat = true ;
        try {
            isCreat = roomDao.ValidateName(username);
        }
         catch (Exception e) {
            e.printStackTrace();
        }
        return isCreat;
    }

    /**
     * room信息注册
     * @param username,password,phone,community_id,housernum,ownername,housecard_img
     * @return int
     * @author 夏添
     * @date 2020/7/15 21:33
     */
    @Override
    public int RegisteredRoom(String username,String password,int community_id,String housernum,String ownername,String phone,String housecard_img){
        EncryptUtil en =new EncryptUtil();
        password = en.getMD5(password);
        int i = 0;
       try{
           i = roomDao.RegisteredRoom(username,password,community_id,housernum,ownername,phone,housecard_img);
       }catch (Exception e){
           e.printStackTrace();
       }
       return i;
    }
    /**
     * 前台登录验证
     * @param username,password
     * @return room
     * @author 夏添
     * @date 2020/7/18 21:33
     */
    @Override
    public Room verify(String username , String password) {
        EncryptUtil en = new EncryptUtil();
        password = en.getMD5(password);
        Room room =new Room();
        try {
            room = roomDao.verify(username, password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return room;
    }

    /**
     * 住房信息检索
     * @param page,viewpower
     * @return ArrayList
     * @author 夏添
     * @date 2020/7/20 20:16
     */
    @Override
    public ArrayList<Room> findByPage(Page page, String view_power) {
        if(view_power.equals("all")){
            view_power = "";
            CommunityDao communityDao = new CommunityDaoImpl();
            try {
                List<Community> list = communityDao.getCommunityList();
                for (int i =0; i<list.size();i++){
                    view_power += list.get(i).getCommunity_id()+",";
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (view_power.equals("")||view_power.equals("null")||view_power.equals("Null"))
            view_power = null ;
        ArrayList<Room> list =new ArrayList<Room>();
        try {
            list = roomDao.findByPage(page,view_power);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 住房信息搜索
     * @param page,community_id,query
     * @return ArrayList
     * @author 夏添
     * @date 2020/7/20 20:32
     */
    @Override
    public ArrayList<Room> QueryRoom(Page page,int community_id,String query,String view_power){
        if(view_power.equals("all")){
            view_power = "";
            CommunityDao communityDao = new CommunityDaoImpl();
            try {
                List<Community> list = communityDao.getCommunityList();
                for (int i =0; i<list.size();i++){
                    view_power += list.get(i).getCommunity_id()+",";
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (view_power .equals( "null") || view_power.equals( "Null"))
            view_power = null;
        ArrayList<Room> list = new ArrayList<Room>();
        try {
            list = roomDao.QueryRoom(page,community_id,query,view_power);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 更新住房信息
     * @param room_id,phone,community_id,housernum,ownername,housecard_img
     * @return int
     * @author 夏添
     * @date 2020/7/20 21:19
     */
    @Override
    public  int UpdateInfo(int room_id,String phone,String ownername,String hosecard_img){
        int i = 0;
        try {
            i = roomDao.UpdateInfo(room_id,phone,ownername,hosecard_img);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 审核住房信息
     * @param room_id
     * @return int
     * @author 夏添
     * @date 2020/7/20 21:24
     */
    @Override
    public int StateYes(int room_id) {
        int i =0;
        try {
            i = roomDao.StateYes(room_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 拒绝住房信息
     * @param room_id
     * @return int
     * @author 夏添
     * @date 2020/7/20 21:26
     */
    @Override
    public int StateNo(int room_id) {
        int i =0;
        try {
            i = roomDao.StateNo(room_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 获取所有允许查看的住房数据总数
     * @param view_power
     * @return int
     * @author 夏天
     * @date 2020/7/21 15:41
     */
    @Override
    public int getCount(String view_power){
        int num=0;
        System.out.println("view_power="+view_power);
        if(view_power.equals("all")){
            view_power = "";
            CommunityDao communityDao = new CommunityDaoImpl();
            try {
                List<Community> list = communityDao.getCommunityList();
                for (int i =0; i<list.size();i++){
                    view_power += list.get(i).getCommunity_id()+",";
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (view_power ==null|| view_power.equals("") || view_power.equals("null"))
            return 0;
        else {
            try {
                num = roomDao.getCount(view_power);
            }catch (Exception e){
                e.printStackTrace();
            }
            return num;
        }
    }
    /**
     * 获取条件查询的住房数据总数
     * @param community_id,query,view_power
     * @return int
     * @author 夏天
     * @date 2020/7/21 15:41
     */
    @Override
    public int getCount(int community_id,String query,String view_power) {
        if(view_power.equals("all")){
            view_power = "";
            CommunityDao communityDao = new CommunityDaoImpl();
            try {
                List<Community> list = communityDao.getCommunityList();
                for (int i =0; i<list.size();i++){
                    view_power += list.get(i).getCommunity_id()+",";
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        int i = 0;
        if ( view_power.equals("null") || view_power.equals( "Null"))
            view_power = null;
        try{
            i = roomDao.getCount(community_id,query,view_power);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  i ;
    }
}
